package com.team1.supervend.domain;

import java.io.BufferedInputStream;
import java.util.HashMap;
/*
//Needed to read a string from console using BufferedReader.
import java.io.*;
*/
import java.util.Scanner;

public class SuperVend {
	private ProductInventory currentProductInventory;
	private SaleHandler currentSaleHandler;
	private MaintenanceHandler currentMaintenanceHandler;
	Money  cashTendered;
	Credit creditTendered;
	SuperVend superVend;
	ProductInventory superVendProductInventory;
	//PurchaseOrder superVendCurrentPurchaseOrder;
	SaleHandler superVendSaleHandler;
	HashMap<ItemID, ProductDescription> productDescriptions;
	ItemID id;
	int quantity;
	float balance;
	boolean preValidatedCashPayment;
	boolean maint;
	boolean prepaymentValid;
	
	public SuperVend(){
			CreateProductInventory();
			makeNewMaintenanceHandler();
			System.out.println("Welcome To SuperVend");

	}
	//  For future use with external input product loading, console uses maintenance handler directly
	public void addProductInventory(int id, float cost, String desc, int qty, int minqty){
		if (maint){
		currentMaintenanceHandler.addProductInventory(id, cost, desc, qty, minqty);
		}
		else {
			System.out.println("sorry system not in maintenance mode");
		}
	}

	public void CreateProductInventory(){
		//System.out.println("SuperVend creates new ProductInventory");
		currentProductInventory = new ProductInventory();
	}
	
	public void makeNewSaleHandler(){
		currentSaleHandler = new SaleHandler();
	}
	
	public SaleHandler getCurrentSaleHandler(){
		return currentSaleHandler;
	}
	public void makeNewMaintenanceHandler(){
		currentMaintenanceHandler = new MaintenanceHandler(currentProductInventory);
	}
	
	public MaintenanceHandler getCurrentMaintenanceHandler(){
		return currentMaintenanceHandler;
	}	
	
	public ProductInventory getCurrentProductInventory()
    {
    	return currentProductInventory;
    }

	public void displayProductInventory( )
	{
		currentProductInventory.printProductDescriptions();
	}

	public void selectItem( int pid, int quantity )
	{
		if (prepaymentValid){
		ItemID id;
		id = getCurrentProductInventory().getItemID(pid);
		ProductDescription desc = currentProductInventory.getProductDescription(id);
		currentSaleHandler.getCurrentPurchaseOrder().makeSelectedItem ( desc, quantity );
		System.out.println("("+quantity+")"+desc.getDescription()+" Added to order");
		}
		else{
			System.out.println("please first insert payment");
		}
	}
	public void selectItems(){
		System.out.println("Please enter item id and qty desired from the menu");
		Scanner selectedItem = new Scanner(new BufferedInputStream(System.in));
		selectItem(selectedItem.nextInt(), selectedItem.nextInt());
		saleMenu();
		
	}
	
	public void saleMenu(){
		System.out.println("To add another item press 1, to complete sale press 2, other press 0 to exit");
		Scanner sale = new Scanner(new BufferedInputStream(System.in));
		switch ( sale.nextInt() ){
		case 1:
			selectItems();
			break;
		case 2:
			completeSale();
			break;
		case 0:
			menu();
			break;
		default:
			System.out.println("try again");
			menu();
		}
		
		
	}
	
	public void startSale(){
		//show in stock items to customer
		if (!maint){
			System.out.println("System in regular interaction mode");
			displayProductInventory();
			//request pre payment
			makeNewSaleHandler();
			makePayment();
		}
		else {
				System.out.println("System in maintenance mode");
			}
		}
		
	public void makePayment(){
			System.out.println(" To pay by credit press 1 or 2 for cash, 0 to cancel ");
			Scanner payment = new Scanner(new BufferedInputStream(System.in));
			switch ( payment.nextInt() ){
			case 1:
				if (!prepaymentValid){
				makeCreditPrePayment();
				selectItems();
				}
				else {
					makeCreditPayment();
				}
				break;
			case 2:
				System.out.println("Please enter amount of cash");
				Scanner cash = new Scanner(new BufferedInputStream(System.in));
				if (!prepaymentValid){
				makeCashPayment(cash.nextFloat());
				selectItems();
				}
				else {
					makeCashPayment(cash.nextFloat());
				}
				break;
			case 0:
				menu();
				break;
			default:
				System.out.println("try again");
				menu();
			}
			
		}
	
	
	public void completeSale(){
		if (currentSaleHandler.completeSale()){
			prepaymentValid = false;
		}
		else
		{
			currentSaleHandler.completeSale();
			makePayment();
			currentSaleHandler.completeSale();
			menu();
		}
		
	}
	
	public boolean makeCashPayment(Float payment){
		boolean retval = false;
		Money cash = new Money(payment);
		if (currentSaleHandler.getPaymentHandler().makeCashPayment(cash)){
			currentSaleHandler.getCurrentPurchaseOrder().addPayment(cash);
			retval = true;
			prepaymentValid = true;
			return retval;
		}
		return retval;
	}

	public boolean makeCreditPayment(Float payment){
		boolean retval = false;
		Credit credit = new Credit(payment);
		if (currentSaleHandler.getPaymentHandler().makeCreditPayment(credit)){
			currentSaleHandler.getCurrentPurchaseOrder().addPayment(credit);
			retval = true;
			return retval;
		}
		return retval;
	}
	
	public boolean makeCreditPayment(){
		boolean retval = false;
		float payment = 0.0f; 
				if (currentSaleHandler.getCurrentPurchaseOrder().getTotal().get() - currentSaleHandler.getCurrentPurchaseOrder().availableMoney > 0.0f){
					payment = currentSaleHandler.getCurrentPurchaseOrder().getTotal().get() - currentSaleHandler.getCurrentPurchaseOrder().availableMoney;
				}
				else {
					payment = currentSaleHandler.getCurrentPurchaseOrder().getTotal().get();
				}
		Credit credit = new Credit(payment);
		if (currentSaleHandler.getPaymentHandler().makeCreditPayment(credit)){
			currentSaleHandler.getCurrentPurchaseOrder().addPayment(credit);
			retval = true;
			return retval;
		}
		return retval;
	}
	// this is temporary, the method below needs to call a pre-auth only routine. 
	public boolean makeCreditPrePayment(){
		boolean retval = false;
		if (currentSaleHandler.getPaymentHandler().makeCreditPrePayment()){
			retval = true;
			prepaymentValid = true;
			return retval;
			
		}
		return retval;
	}
	
	public Boolean setMaintMode(int is_Maint) {
		if (currentMaintenanceHandler.setMaintMode(is_Maint)){
			maint = false;
			menu();
		}
		menu();
		return maint;
	}
	public void setMaint(){
	System.out.println("Enter Password");
	Scanner password = new Scanner(new BufferedInputStream(System.in));
	if (setMaintMode(password.nextInt())){
		System.out.println("In Maint");
		maint = false;
		menu();
		}
	else {
		menu(); 
	}
	}
	
	
	
	public void menu(){
	System.out.println("To get started press 1, secret maintenance mode press 0 to log in for your eyes only pw = 1234 ");
	Scanner menu = new Scanner(new BufferedInputStream(System.in));
	switch ( menu.nextInt() ){
	case 1:
		startSale();
		break;
	case 0:
		setMaint();
		maint = false;
		break;
	default:
		System.out.println("try again");
		menu();
	}
}
}
